package goover.unit05;

import java.sql.*;

public class JdbcDemo03 {
    public static void main(String[] args){
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/jdbc";
            String user = "root";
            String password = "root";
            conn = DriverManager.getConnection(url,user,password);
            String sql = "select * from tb_user where id = ?";
            pst = conn.prepareStatement(sql);
            pst.setInt(1,1);
            rs = pst.executeQuery();
            System.out.println("" + "id" + " | " + "name" + " | " + "sex" + " | " + "eamil" + " | " + "birthday");
            while (rs.next()){
                int id = rs.getInt("id");
                String name = rs.getString("name");
                String sex = rs.getString("sex");
                String eamil = rs.getString("email");
                Date birthday = rs.getDate("birthday");
                System.out.println("" + id + " | " + name + " | " + sex + " | " + eamil + " | " + birthday);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally{
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(pst != null){
                try {
                    pst.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
    }
}
